﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using BLL.Entities;

using System.Web.UI.HtmlControls;


namespace GMS
{
    public partial class Pages_Controls_Shape8 : System.Web.UI.UserControl
    {
        private bool errMsgFlag = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                img1.ImageUrl = SessionVariable.Instance.UserDetails.SelectedShapeEnterInputsUrl;
            }
            if (dsA.IsPostBack)
            {
                ToggleBacksplash(dsA);
            }
            if (dsB.IsPostBack)
            {
                ToggleBacksplash(dsB);
            }
            
        }

        private void ToggleBacksplash(Pages_Controls_DimensionSelector dimSelector)
        {
            RadioButtonList rblSeleted = (RadioButtonList)dimSelector.FindControl("rblSideType");
            CheckBox cbBacksplash = (CheckBox)dimSelector.FindControl("cbBacksplash");

            if (rblSeleted.SelectedIndex > 0)
            {
                if (string.Compare(rblSeleted.SelectedItem.Text.ToUpper(), "EDGE") == 0 ||
                    string.Compare(rblSeleted.SelectedItem.Text.ToUpper(), "APPLIANCE") == 0)
                {
                    dimSelector.IsBackSplashEnabled = false;
                    cbBacksplash.Checked = false;
                }
            }
            if (cbBacksplash.Checked == true)
                rblSeleted.SelectedIndex = 0;
        }

        public bool OperationsUC()
        {
            int validationErrorCnt = 0;
            int dimA = 0, dimB = 0;

            SessionVariable.Instance.UserDetails.Cost.Edge = 0;

            validationErrorCnt += AddShapeDimension(dsA, Side.Sides.A, out dimA);
            validationErrorCnt += AddShapeDimension(dsB, Side.Sides.B, out dimB);

            if (errMsgFlag)
            {
                ShowMessage("Enter dimensions(in inches) and select options (wall/appliance/edge) for all the sides mentioned");
            }

            if (validationErrorCnt == 0)
            {
                SessionVariable.Instance.UserDetails.Cost.Surface = CalculateArea(Utility.CalculateShapeSizeIntoFT(dimA),
                                                                                Utility.CalculateShapeSizeIntoFT(dimB)
                                                                                );
                return true;
            }
            return false;
        }

        private double CalculateArea(double dim1, double dim2)
        {
            // side's are in inches
            // convert them into FT
            // multiply to get the SQFT
            return dim1 * dim2;
        }

        private int AddShapeDimension(Pages_Controls_DimensionSelector dsObj, Side.Sides sideLiteral, out int dim)
        {
            TextBox txt = (TextBox)dsObj.FindControl("txtDimension");
            RadioButtonList rbl = (RadioButtonList)dsObj.FindControl("rblSideType");
            CheckBox cb = (CheckBox)dsObj.FindControl("cbBacksplash");
            dim = 0;
            int res = 0;
            int retValue = 1;
            bool andFlag = false;

            if (txt.Enabled)
            {
                if (Int32.TryParse(txt.Text, out res) == false)
                {
                    errMsgFlag = true;
                }
            }
            if (rbl.SelectedIndex == -1)
            {
                errMsgFlag = true;
            }
            
            if (
                andFlag == false &&
                rbl != null &&
                cb != null &&
                rbl.SelectedIndex > -1)
            {
                // Store all these sidetypes into Session
                List<BLL.Entities.ShapeDimension> sdList = new List<BLL.Entities.ShapeDimension>();

                if (SessionVariable.Instance.UserDetails.Shape != null)
                {
                    sdList = SessionVariable.Instance.UserDetails.Shape;
                }

                // A Side
                ShapeDimension sD = new ShapeDimension();
                sD.IsBackSplash = cb.Checked;
                if (txt.Visible == true && !string.IsNullOrEmpty(txt.Text))
                    int.TryParse(txt.Text, out dim);
                sD.Measurement = dim;
                sD.SideTypeId = Convert.ToInt32(rbl.SelectedValue);
                sD.shapeId = SessionVariable.Instance.UserDetails.ShapeId;
                sD.Side = sideLiteral;
                sdList.Add(sD);

                SessionVariable.Instance.UserDetails.Shape = sdList;
                // SFT. means x inches/144
                if (cb.Checked)
                    SessionVariable.Instance.UserDetails.Cost.Backsplash += Convert.ToInt32(
                        GMS.Utility.CalculateShapeSizeIntoFT(Convert.ToInt32(GMS.Utility.CalculateShapeSizeIntoFT(dim * 4))));

                if (string.Compare(rbl.SelectedItem.Text.ToUpper(), "EDGE") == 0)
                {
                    SessionVariable.Instance.UserDetails.Cost.Edge += Convert.ToInt32(GMS.Utility.CalculateShapeSizeIntoFT(dim));
                }
                retValue = 0;
            }
            return retValue;
        }

        private void ShowMessage(string msgText)
        {
            HtmlGenericControl divMsg = (HtmlGenericControl)Page.Master.FindControl("divMsg");

            if (divMsg != null)
            {
                Control msg = this.Page.LoadControl("Controls/Message.ascx");

                if (msg != null)
                {
                    Label lblMessage = (Label)msg.FindControl("lblMessage");

                    if (lblMessage != null)
                    {
                        lblMessage.Text = msgText;
                    }

                    divMsg.Controls.Add(msg);
                }
            }

        }

    }
}